package com.abc.client;

import java.awt.ScrollPane;
import java.awt.Window;
import java.util.HashMap;
import java.util.Set;

import com.abc.shared.Classification;
import com.abc.shared.ClassifiedItem;
import com.abc.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.TextArea;
import com.mysql.jdbc.UpdatableResultSet;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Classific implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	
	
	private String State="Begin";
	private final TextBox textName = new TextBox();
	private final Tree classificationtree = new Tree();
	private final  TextBox textSmsText = new TextBox();
	private final  TextBox textRank = new TextBox();
	private final  TextBox textServicetype = new TextBox();
	private final  TextArea textKeywords = new TextArea();
	private final Button botton1 = new Button("New button");
	private final Button botton2 = new Button("New button");
	private final Button botton3 = new Button("New button");
	private final  TextBox textID = new TextBox();
	private final  TextBox textLevel = new TextBox();
	private final Label lblLogitudemaxPrice = new Label("Longitude/Max Price:");
	private final TextBox textLongMax = new TextBox();
	private final TextBox textLatMin = new TextBox();
	private final Label lblLatitudeminPrice = new Label("Latitude/Min Price :");
	private final Label lblPrompt = new Label("Prompt:");
	private final TextBox textPrompt = new TextBox();
	private final Button botton4 = new Button("New button");
	private TreeItem movingItem = null;
	private TreeItem newParent=null;
	private final HTML htmlNewHtml = new HTML("New HTML", true);
	private final FileUpload promptUpload = new FileUpload();
	
	public void onModuleLoad() {
		
		
		
		// Component Definition
		
		textName.setName("Enter the Name");				
		ScrollPanel treePanel = new ScrollPanel();
		treePanel.setSize("300px", "410px");
		
		AbsolutePanel formPanel = new AbsolutePanel();
		formPanel.setSize("612px", "426px");
		
		//Ading to the Root Panel
		
		RootPanel.get("classificationTreeContainer").add(treePanel);		
		treePanel.add(classificationtree);
		RootPanel.get("formcontainer").add(formPanel);
					
		formPanel.add(textName, 238, 30);		
		textName.setSize("122px", "15px");
		
		Label lblNewLabel = new Label("Name:");
		formPanel.add(lblNewLabel, 238, 10);
		
		Label lblSmsText = new Label("SMS Text:");
		formPanel.add(lblSmsText, 28, 80);
		lblSmsText.setSize("83px", "15px");
		
		
		textSmsText.setName("Enter the Name");
		formPanel.add(textSmsText, 28, 100);
		textSmsText.setSize("122px", "15px");
		
		Label lblKeyWords = new Label("Key Words :");
		formPanel.add(lblKeyWords, 28, 278);
		lblKeyWords.setSize("103px", "15px");
		
		Label lblRank = new Label("Rank:");
		formPanel.add(lblRank, 28, 150);
		lblRank.setSize("39px", "15px");
		
		
		textRank.setName("Enter the Name");
		formPanel.add(textRank, 28, 170);
		textRank.setSize("122px", "15px");
		
		Label lblServiceType = new Label("Service Type:");
		formPanel.add(lblServiceType, 238, 150);
		lblServiceType.setSize("132px", "15px");
		
		textServicetype.setReadOnly(true);
		textServicetype.setName("Enter the Name");
		formPanel.add(textServicetype, 238, 170);
		textServicetype.setSize("122px", "15px");
		
		formPanel.add(textKeywords, 28, 299);
		textKeywords.setSize("332px", "56px");
		


		classificationtree.setAnimationEnabled(true);
		
		botton1.setText("Edit");
		formPanel.add(botton1, 28, 389);
		botton1.setSize("109px", "27px");
		
		
		
		botton2.setText("Add Child");
		formPanel.add(botton2, 181, 389);
		botton2.setSize("100px", "27px");
		
		
		botton3.setText("Move Branch");
		formPanel.add(botton3, 321, 389);
		botton3.setSize("109px", "27px");
		
		Label lblId = new Label("ID:");
		formPanel.add(lblId, 28, 10);
		lblId.setSize("83px", "15px");
		
		
		textID.setReadOnly(true);
		textID.setName("Enter the Name");
		formPanel.add(textID, 28, 30);
		textID.setSize("122px", "15px");
		
		Label lblLevel = new Label("Level:");
		formPanel.add(lblLevel, 238, 80);
		lblLevel.setSize("39px", "15px");
		
		
		textLevel.setReadOnly(true);
		textLevel.setName("Enter the Name");
		formPanel.add(textLevel, 238, 100);
		textLevel.setSize("122px", "15px");
		
		formPanel.add(lblLogitudemaxPrice, 450, 80);
		lblLogitudemaxPrice.setSize("132px", "15px");
		textLongMax.setName("Enter the Name");
		
		formPanel.add(textLongMax, 450, 100);
		textLongMax.setSize("122px", "15px");
		textLatMin.setReadOnly(true);
		textLatMin.setName("Enter the Name");
		
		formPanel.add(textLatMin, 450, 30);
		textLatMin.setSize("122px", "15px");
		
		formPanel.add(lblLatitudeminPrice, 450, 10);
		lblLatitudeminPrice.setSize("132px", "15px");
		
		formPanel.add(lblPrompt, 28, 220);
		lblPrompt.setSize("132px", "15px");
		textPrompt.setReadOnly(true);
		textPrompt.setName("Enter the Name");
		
		formPanel.add(textPrompt, 28, 240);
		textPrompt.setSize("122px", "15px");
		
		botton4.setText("Delete");
		
		formPanel.add(botton4, 473, 389);
		botton4.setSize("109px", "27px");
		
		formPanel.add(htmlNewHtml, 181, 240);
		htmlNewHtml.setSize("132px", "27px");
		
		FormPanel formPanel_1 = new FormPanel();
		formPanel.add(formPanel_1, 353, 240);
		formPanel_1.setSize("151px", "27px");
		formPanel_1.setWidget(promptUpload);
		promptUpload.setSize("166px", "23px");
		
		updateStatus("Begin");
		
		// Handlers
		    
		
		botton1.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				if (State.equals("ItemSelected")){
					updateStatus("Edit");
				}
				else if (State.equals("Edit")){
					
					ClassifiedItem item = new ClassifiedItem((Integer)classificationtree.getSelectedItem().getUserObject(), (Integer)classificationtree.getSelectedItem().getParentItem().getUserObject(), textName.getText(), "", Integer.parseInt(textRank.getText()), textKeywords.getText(), getServiceType(), calculateLevel(), textSmsText.getText(), Double.parseDouble(textLongMax.getText()), Double.parseDouble(textLatMin.getText()));
					updateItem(item);
					updateStatus("ItemSelected");
				}
				else if (State.equals("Add Child")){
					
					if (!textName.getText().equals("")){
						ClassifiedItem item = new ClassifiedItem(0, (Integer)classificationtree.getSelectedItem().getUserObject(), textName.getText(), "", Integer.parseInt(textRank.getText()), textKeywords.getText(), getServiceType(), calculateLevel()+1, textSmsText.getText(), Double.parseDouble(textLongMax.getText()), Double.parseDouble(textLatMin.getText()));
						addItem(item);
						updateStatus("ItemSelected");
					}
					
				}
				else if (State.equals("Move")){	// Cancel
					updateStatus("ItemSelected");
				}
				else if (State.equals("Paste Branch")){
					movingItem.getParentItem().removeItem(movingItem);
					newParent.addItem(movingItem);
					newParent.setState(true);
					newParent.setSelected(false);
					//movingItem.setSelected(true);	
					
					
					
					updateStatus("ItemSelected");
					classificationtree.setSelectedItem(movingItem, true);
					greetingService.updateParent((Integer)newParent.getUserObject(), (Integer)movingItem.getUserObject(), new AsyncCallback<Boolean>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Boolean result) {
							// TODO Auto-generated method stub
							
						}
					});
				
					updateForm((Integer)movingItem.getUserObject());
				}
				
			}
		});
		
		
		
		botton2.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				if (State.equals("Edit")){
					updateStatus("ItemSelected");
					updateForm((Integer)classificationtree.getSelectedItem().getUserObject());
				}
				else if (State.equals("ItemSelected")||State.equals("RootSelected")){	// Ad Child
					updateStatus("Add Child");
				}
				else if (State.equals("Paste Branch")){	// Cancel
					updateStatus("ItemSelected");
				}
				else if (State.equals("Add Child")){	// Cancel
					updateForm((Integer)classificationtree.getSelectedItem().getUserObject());
					updateStatus("ItemSelected");
				}
				
			}
		});
		
		
		
		botton3.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (State.equals("ItemSelected")){
					updateStatus("Move");
					movingItem=classificationtree.getSelectedItem();
					newParent=null;
				}
			}
		});
		
		
		botton4.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				if (State.equals("ItemSelected")){
					
					if(com.google.gwt.user.client.Window.confirm("Are you sure you want to Delete this branch ?")){
						final TreeItem current = classificationtree.getSelectedItem();
						int item= (Integer)current.getUserObject();
						final int parent=(Integer)current.getParentItem().getUserObject();
						
						greetingService.deleteBranch(item, new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onSuccess(Boolean result) {
								current.getParentItem().setSelected(true);
								current.remove();
								updateForm(parent);
								
							}
						});
						
						
						
					}
					
					
					//updateForm((Integer)classificationtree.getSelectedItem().getUserObject());
					//updateStatus("ItemSelected");
				}
			}
		});
		
		
		
		classificationtree.addSelectionHandler(new SelectionHandler<TreeItem>() {
			
			@Override
			public void onSelection(SelectionEvent<TreeItem> event) {
				
				if (State.equals("Move")||State.equals("Paste Branch")){
					
					TreeItem item =  classificationtree.getSelectedItem();
					
					if (getMovingBranch().equals(item)){
						// same selected
						item.setSelected(false);
						movingItem.setSelected(true);
						updateForm((Integer)movingItem.getUserObject());
						updateStatus("Move");
						
					}
					else if (isChildOf(item, getMovingBranch())){
						item.setSelected(false);
						movingItem.setSelected(true);
						updateForm((Integer)movingItem.getUserObject());
						updateStatus("Move");
					}
					else{
						movingItem.setSelected(false);
						updateForm((Integer)event.getSelectedItem().getUserObject());
						newParent=event.getSelectedItem();
						updateStatus("Paste Branch");
						
					}
					
					// Selection Constraints Apply
					//updateForm((Integer)event.getSelectedItem().getUserObject());
		
				}
				else{
					if ((Integer)event.getSelectedItem().getUserObject()!=0)
					{
						updateForm((Integer)event.getSelectedItem().getUserObject());
						updateStatus("ItemSelected");
					
					}
					else{
						updateStatus("RootSelected");
					}
		
				}
				
				
							
			}
		});
		
		
	    
		greetingService.getClassification(new AsyncCallback<Classification<Classification>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(	Classification<Classification> results) {
				TreeItem treeroot =new TreeItem() ;
				treeroot.setText(results.getName());
				treeroot.setUserObject(results.getItemId());
				classificationIttorator(results, treeroot);
				classificationtree.addItem(treeroot);
				
			}

		
		
			
			
		});
	    	    
	    	   
		

		
	}
	
	private Boolean isChildOf(TreeItem child, TreeItem parent){
		
		int length=parent.getChildCount();
		for (int i=0;i<length;i++){
			
			if (child.equals(parent.getChild(i)))
				return true;
			else if (isChildOf(child, parent.getChild(i)))
				return true;
		}
		
		
		return false;
	}
	
	private void classificationIttorator (Classification<Classification> classific, TreeItem tree){
				
		for (Classification<Classification> item : classific) {
			TreeItem newtreeitem = new TreeItem();
			newtreeitem.setText(item.getName());
			newtreeitem.setUserObject(item.getItemId());			
			tree.addItem(newtreeitem);			
			classificationIttorator(item,newtreeitem);
			
			
		}
	}
	
	private String getChildren(TreeItem tree){
	
		
		return "";
	}
	
	
	private void updateForm(int itemid){

		if(itemid==0)
			return ;
		
		greetingService.getClassifiedItem(itemid, new AsyncCallback<ClassifiedItem>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ClassifiedItem result) {
				textName.setText(result.getName());
				textKeywords.setValue(result.getKeywords(),false);
				textRank.setText(String.valueOf(result.getRank()));
				textSmsText.setText(result.getSmsText());
				textServicetype.setText(result.getServiceType());
				textID.setText(String.valueOf(result.getItemID()));
				textLevel.setText(String.valueOf(result.getLevel()));
				textLongMax.setText(String.valueOf(result.getLngtdeMaxprize()));
				textLatMin.setText(String.valueOf(result.getLttudeMinprize()));
				textPrompt.setText(result.getPrompt());
				
				htmlNewHtml.setHTML("<EMBED SRC=http://localhost:8888/"+result.getPrompt()+".gsm WIDTH=132 HEIGHT = 30 AUTOPLAY=false CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/>");
				
				
				
			}
		});
		

	}
	
	private void updateItem(ClassifiedItem item){

		classificationtree.getSelectedItem().setText(item.getName());
		greetingService.updateItem(item, new AsyncCallback<ClassifiedItem>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ClassifiedItem result) {
				textName.setText(result.getName());
				textKeywords.setValue(result.getKeywords(),false);
				textRank.setText(String.valueOf(result.getRank()));
				textSmsText.setText(result.getSmsText());
				textServicetype.setText(result.getServiceType());
				textID.setText(String.valueOf(result.getItemID()));
				textLevel.setText(String.valueOf(result.getLevel()));
				textLongMax.setText(String.valueOf(result.getLngtdeMaxprize()));
				textLatMin.setText(String.valueOf(result.getLttudeMinprize()));
				textPrompt.setText(result.getPrompt());
				
				htmlNewHtml.setHTML("<EMBED SRC=http://localhost:8888/"+result.getPrompt()+".gsm WIDTH=132 HEIGHT = 30 AUTOPLAY=false CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/>");
				
			}
		});
		

	}
	
	private TreeItem getMovingBranch(){
		return movingItem;
	}
	
	
	private void setMovingBranch(TreeItem branch){
		movingItem=branch;
	}
	
	private void setNewParent(TreeItem parent){
		newParent=parent;
	}
	
	
	private void addItem(ClassifiedItem item){

		
		greetingService.addItem(item, new AsyncCallback<ClassifiedItem>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ClassifiedItem result) {
				textName.setText(result.getName());
				textKeywords.setValue(result.getKeywords(),false);
				textRank.setText(String.valueOf(result.getRank()));
				textSmsText.setText(result.getSmsText());
				textServicetype.setText(result.getServiceType());
				textID.setText(String.valueOf(result.getItemID()));
				textLevel.setText(String.valueOf(result.getLevel()));
				textLongMax.setText(String.valueOf(result.getLngtdeMaxprize()));
				textLatMin.setText(String.valueOf(result.getLttudeMinprize()));
				textPrompt.setText(result.getPrompt());
				
				htmlNewHtml.setHTML("<EMBED SRC=http://localhost:8888/"+result.getPrompt()+".gsm WIDTH=132 HEIGHT = 30 AUTOPLAY=false CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/>");
				
				TreeItem newitem = new TreeItem();
				newitem.setText(result.getName());
				newitem.setUserObject(result.getItemID());
				classificationtree.getSelectedItem().addItem(newitem);
				classificationtree.getSelectedItem().setState(true);
				classificationtree.getSelectedItem().setSelected(false);
				newitem.setSelected(true);
				
				
			}
		});
		
		
	}

	
	private int calculateLevel(){
		
		int level=0;
		TreeItem item=classificationtree.getSelectedItem();
		while (true){		
			if (item!=null){
				level++;
			}
			else{
				return level-2;
			}
			
			item =item.getParentItem();			
		}	
		
	}
	
	private String getServiceType(){
		
		int level=0;
		TreeItem item=classificationtree.getSelectedItem();
		if ((Integer)item.getUserObject()==0){
			return "";
		}
		
		while (true){
			String name=item.getText();
			item =item.getParentItem();	
			if ((Integer)item.getUserObject()==0){
				return name;
			}
					
		}	
		
	}
	
	private void updateStatus(String state){
		
		State=state;
		if (State.equals("Begin")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(true);
			textLevel.setReadOnly(true);
			textName.setReadOnly(true);
			textRank.setReadOnly(true);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(true);	
			textLongMax.setReadOnly(true);	
			textLatMin.setReadOnly(true);	
			
			botton2.setText("Add Child");
			botton3.setText("Move Branch");
			botton1.setText("Edit");
			botton4.setText("Delete");
			
			botton2.setVisible(false);
			botton3.setVisible(false);
			botton1.setVisible(false);
			botton4.setVisible(false);
			
			textID.setText("");
			textKeywords.setText("");
			textLevel.setText("");
			textName.setText("");
			textRank.setText("");
			textServicetype.setText("");
			textSmsText.setText("");
			textLongMax.setText("");
			textLatMin.setText("");
			htmlNewHtml.setVisible(false);
			promptUpload.setVisible(false);
			
		}
		else if (State.equals("ItemSelected")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(true);
			textLevel.setReadOnly(true);
			textName.setReadOnly(true);
			textRank.setReadOnly(true);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(true);	
			textLongMax.setReadOnly(true);	
			textLatMin.setReadOnly(true);	
			
			botton2.setText("Add Child");
			botton3.setText("Move Branch");
			botton1.setText("Edit");
			botton4.setText("Delete");
			
			botton2.setVisible(true);
			botton3.setVisible(true);
			botton1.setVisible(true);
			botton4.setVisible(true);
			htmlNewHtml.setVisible(true);
			promptUpload.setVisible(false);
			
		}
		else  if (State.equals("Edit")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(false);
			textLevel.setReadOnly(true);
			textName.setReadOnly(false);
			textRank.setReadOnly(false);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(false);	
			textLongMax.setReadOnly(false);	
			textLatMin.setReadOnly(false);	
			
			botton2.setText("Cancel");
			botton3.setText("Move Branch");
			botton1.setText("Update");
			
			botton2.setVisible(true);
			botton3.setVisible(false);
			botton4.setVisible(false);
			botton1.setVisible(true);
			htmlNewHtml.setVisible(true);
			promptUpload.setVisible(true);
			
		}
		else if (State.equals("Move")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(true);
			textLevel.setReadOnly(true);
			textName.setReadOnly(true);
			textRank.setReadOnly(true);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(true);	
			textLongMax.setReadOnly(true);	
			textLatMin.setReadOnly(true);	
			
			botton2.setText("Cancel");
			botton3.setText("Move Branch");
			botton1.setText("Cancel");
			botton4.setText("Delete");
			
			botton2.setVisible(false);
			botton3.setVisible(false);
			botton1.setVisible(true);
			botton4.setVisible(false);
			htmlNewHtml.setVisible(true);
			promptUpload.setVisible(false);
		}
		else if (State.equals("Paste Branch")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(true);
			textLevel.setReadOnly(true);
			textName.setReadOnly(true);
			textRank.setReadOnly(true);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(true);	
			textLongMax.setReadOnly(true);	
			textLatMin.setReadOnly(true);	
			
			botton2.setText("Cancel");
			botton3.setText("Move Branch");
			botton1.setText("Paste Branch");
			botton4.setText("Delete");
			
			botton2.setVisible(true);
			botton3.setVisible(false);
			botton1.setVisible(true);
			botton4.setVisible(false);
			htmlNewHtml.setVisible(true);
			promptUpload.setVisible(false);
		}
		else if (State.equals("Add Child")){
			
			textID.setReadOnly(true);
			textKeywords.setReadOnly(false);
			textLevel.setReadOnly(true);
			textName.setReadOnly(false);
			textRank.setReadOnly(false);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(false);	
			textLongMax.setReadOnly(false);	
			textLatMin.setReadOnly(false);	
			
			botton2.setText("Cancel");
			botton3.setText("Move Branch");
			botton1.setText("Save");
			
			botton2.setVisible(true);
			botton3.setVisible(false);
			botton4.setVisible(false);
			botton1.setVisible(true);
			
			textID.setText("0");
			textKeywords.setText("");
			textLevel.setText(String.valueOf((calculateLevel()+1)));
			textName.setText("");
			textRank.setText("0");
			textServicetype.setText(getServiceType());
			textSmsText.setText("");
			textLongMax.setText("0");
			textLatMin.setText("0");
			htmlNewHtml.setVisible(false);
			promptUpload.setVisible(false);
		}
		else if (State.equals("RootSelected")){
			textID.setReadOnly(true);
			textKeywords.setReadOnly(true);
			textLevel.setReadOnly(true);
			textName.setReadOnly(true);
			textRank.setReadOnly(true);
			textServicetype.setReadOnly(true);
			textSmsText.setReadOnly(true);	
			textLongMax.setReadOnly(true);	
			textLatMin.setReadOnly(true);	
			
			botton2.setText("Add Child");
			botton3.setText("Move Branch");
			botton1.setText("Edit");
			botton4.setText("Delete");
			
			botton2.setVisible(true);
			botton3.setVisible(false);
			botton1.setVisible(false);
			botton4.setVisible(false);
			htmlNewHtml.setVisible(false);
			promptUpload.setVisible(false);
			
			textID.setText("");
			textKeywords.setText("");
			textLevel.setText("");
			textName.setText("");
			textRank.setText("");
			textServicetype.setText("");
			textSmsText.setText("");
			textLongMax.setText("");
			textLatMin.setText("");
			
			
		}
		
		
		
	}
}
